<?php

namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\filters\VerbFilter;
use app\models\ContactForm;
use app\models\Qa;
use app\models\Article;
use app\controllers\BaseController;

class SiteController extends BaseController {

    public function behaviors() {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    public function actions() {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

    /**
     * 首页
     * @return type
     */
    public function actionIndex() {
        //1、获取最新问答
        $qa_list = Qa::find()->where(['status' => '1',])->orderBy('id desc')->limit(20)->all();
        //2、获取最新文章
        $article_list = Article::find()->where(['status' => '1',])->orderBy('id desc')->limit(20)->all();

        return $this->render('index', ['qa_list' => $qa_list, 'article_list' => $article_list]);
    }

    /**
     * 联系我们页面
     * @return type
     */
    public function actionContact() {
        $model = new ContactForm();
        if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
            Yii::$app->session->setFlash('contactFormSubmitted');

            return $this->refresh();
        } else {
            return $this->render('contact', [
                        'model' => $model,
            ]);
        }
    }

    /**
     * 关于我页面
     * @return type
     */
    public function actionAbout() {
        return $this->render('about');
    }

    /**
     * 消息提示页面
     * @return type
     */
    public function actionMessage() {
        return $this->render('message');
    }

    /**
     * 帮助页面
     * @return type
     */
    public function actionHelp() {
        return $this->render('help');
    }

}
